﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class frm_BackFund : System.Web.UI.Page
{
    private static string strType = "5";//采购退款

    protected void Page_Load(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();

        string strUserID = cls_Property.SetOrGetUserID;

        if (strUserID != "")
        {
            if (!IsPostBack)
            {
                ////////////////按钮权限控制 Begin /////////////////////////
                string strCurrPageID = this.Page.Request.Params["PageID"];
                this.ControlButtonVisible(strUserID, strCurrPageID);
                //////////////////////  End ///////////////////////////////

                this.GetStockInInfoByMain();
            }
            else
            {
                this.labMessage.Text = "";
                this.lblJs.Text = "";//清空Js脚本
            }

            this.ibtnDelete0.Attributes.Add("onClick", "return confirm('您真的要删除所选的项吗？');");
            this.lbtnDelete0.Attributes.Add("onClick", "return confirm('您真的要删除所选的项吗？');");
            this.lbtnDelete1.Attributes.Add("onClick", "return confirm('彻底删除将无法恢复数据。您真的要删除所选的项吗？');");
        }
        else
        {
            ClsMenuDAL cls_Menu = new ClsMenuDAL();
            string strMessage = cls_Menu.GetSystemCode("1", "00", this.ctbAddStoBackNo.Text.Trim(), 12);//取消当前系统编码
            this.Page.Session.Remove("SetOrGetUserID");
            this.Page.RegisterClientScriptBlock("Js", "<script>parent.window.location.href='../index.aspx'</script>");
        }
    }

    public void ChangPage(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        DataSet dst = cls_Property.SetOrGetCorporationDataSet;

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            string strKey = ((LinkButton)sender).CommandName;
            int intPageIndex = this.dgrdResult.CurrentPageIndex;
            int intPageCounts = this.dgrdResult.PageCount;

            if (strKey == "0")
            {
                intPageIndex = 0;
            }
            else if (strKey == "1")
            {
                if (intPageIndex > 0) intPageIndex -= 1;
            }
            else if (strKey == "2")
            {
                if (intPageIndex < intPageCounts - 1) intPageIndex += 1;
            }
            else
            {
                intPageIndex = intPageCounts - 1;
            }

            this.BindStockInByDataView(dst, intPageIndex);
        }
    }

    public void ChildChangPage(object sender, EventArgs e)
    {
        //获取保存的从表中的结果集和DataGrid控件
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        DataSet dst = cls_Property.SetOrGetChildResultDataSet;
        DataGrid dgrd = cls_Property.SetOrGetChildDataGridControl;

        if (dst != null && dgrd != null)
        {
            string strKey = ((LinkButton)sender).CommandName;
            int intPageIndex = dgrd.CurrentPageIndex;
            int intPageCounts = dgrd.PageCount;

            if (strKey == "0")
            {
                intPageIndex = 0;
            }
            else if (strKey == "1")
            {
                if (intPageIndex > 0) intPageIndex -= 1;
            }
            else if (strKey == "2")
            {
                if (intPageIndex < intPageCounts - 1) intPageIndex += 1;
            }
            else
            {
                intPageIndex = intPageCounts - 1;
            }

            this.BindStockInChildByDataView(dgrd, dst, intPageIndex);
        }
    }

    public void AddStockInInfo(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();

        string strUserID = cls_Property.SetOrGetUserID;

        if (strUserID != "")
        {
            ImageButton ibtnAdd = (ImageButton)sender;
            int intCommandName = Int32.Parse(ibtnAdd.CommandName);

            if (intCommandName == 0)//打开主表添加面板
            {
                ClsMenuDAL cls_Menu = new ClsMenuDAL();

                string strCode = cls_Menu.GetSystemCode("0", "00", "", 59);//获取系统编码

                if (strCode != "Error")
                {
                    this.panAddResult.Visible = true;
                    this.ctbAddStoBackNo.Text = strCode;
                    this.ctbAddWriter.Text = strUserID;
                    this.ctsbStoBackDate.Text = System.DateTime.Today.ToShortDateString();
                    this.ctbAddOper_Date.Text = System.DateTime.Today.ToShortDateString();
                    this.lblJs.Text = "<script>document.getElementById('" + this.ctsbStoBackDate.ClientID + "_txtBox').focus();</script>";
                }
                else
                {
                    this.Page.RegisterClientScriptBlock("Js", "<script>alert('新增记录失败，请重试！')</script>");
                }
            }
            else if (intCommandName == 1)//关闭主表添加面板
            {
                this.panAddResult.Visible = false;
            }
            else if (intCommandName == 2)//保存主表添加记录
            {                
                string strBack_no = this.ctbAddStoBackNo.Text.Trim();//采购退款单号
                DateTime dBackDate = DateTime.Parse(this.ctsbStoBackDate.Text.Trim());//退款日期
                string strProvide = this.ctsbStoBackProvide.Text.Trim();//供应商
                string strDept = this.ctsbAddDept.Text.Trim();//部门
                string strOperationer = this.ctsbOperationer.Text.Trim();//业务员
                string strBalanceType = this.ctsbBalanceType.Text.Trim();//结算方式
                string strWriter = this.ctbAddWriter.Text.Trim();//录单人
                DateTime dOper_Date = DateTime.Parse(this.ctbAddOper_Date.Text.Trim());//录单日期                
                string strRemark = this.ctbAddRemark.Text.Trim();//备注

                ClsPayOutFundDAL cls_PayOutFund = new ClsPayOutFundDAL();
                Boolean bolIsSucceed = cls_PayOutFund.AddPayOutFund(strType, strBack_no, dBackDate, strProvide, strDept,
                    strOperationer, strBalanceType,strWriter, dOper_Date, strRemark);
                if (bolIsSucceed)
                {
                    this.panAddResult.Visible = false;
                    this.GetStockInInfoByMain();//重新绑定结果   

                    //主表新增后直接打开从表进行新增从表记录 Begin///////////////////////////
                    this.panAddResult.Visible = false;
                    ImageButton ibtn = new ImageButton();

                    int intItems = this.dgrdResult.Items.Count - 1;

                    for (int i = intItems; i >= 0; i--)
                    {
                        string strBack_NO = ((CuTextBox)this.dgrdResult.Items[i].FindControl("ctbRsBack_no")).Text.Trim();//采购退款单号

                        if (strBack_NO == strBack_no)
                        {
                            ibtn = (ImageButton)this.dgrdResult.Items[i].FindControl("ibtnOpenChild");
                            break;
                        }
                    }

                    this.OpenChildResult(ibtn, null);
                    this.AddStockInInfo(this.ibtnAdd, null);
                    //主表新增后直接打开从表进行新增从表记录 End///////////////////////////
                }
                else
                {
                    this.Page.RegisterClientScriptBlock("Js", "<script>alert('新增记录失败，请重试！')</script>");
                }
            }
            //////////////////////////////////////////从表相关操作/////////////////////////////////////
            else
            {
                int intIndex = Int32.Parse(this.ibtnAdd.CommandArgument);

                Panel panChildAddResult = (Panel)this.dgrdResult.Items[intIndex].FindControl("panChildAddResult");

                if (intCommandName == 3)//关闭从表添加面板
                {
                    panChildAddResult.Visible = false;
                }
                else if (intCommandName == 4)//保存从表添加记录
                {
                    CuTextBox ctbRsBack_no = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbRsBack_no");//采购退款单号
                    CuTextSelectBox ctsbRsBack_date = (CuTextSelectBox)this.dgrdResult.Items[intIndex].FindControl("ctsbRsBack_date");//退款日期
                    CuTextSelectBox ctsbChildAddStoBack_no = (CuTextSelectBox)this.dgrdResult.Items[intIndex].FindControl("ctsbChildAddStoBack_no");//采购退货单号
                    CuTextBox ctbChildAddStoBack_date = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbChildAddStoBack_date");//退货日期
                    CuTextBox ctbRemark = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbChildAddRemark");//备注

                    ClsPayOutFundDAL cls_PayOutFund = new ClsPayOutFundDAL();
                    Boolean bolIsSucceed = cls_PayOutFund.AddStockBackFundChild(strType, ctbRsBack_no.Text.Trim(), DateTime.Parse(ctsbRsBack_date.Text.Trim()),
                                            ctsbChildAddStoBack_no.Text.Trim(), DateTime.Parse(ctbChildAddStoBack_date.Text.Trim()), ctbRemark.Text.Trim(), 0);
                    

                    if (bolIsSucceed)
                    {
                        panChildAddResult.Visible = false;                   
                        DataSet dst = cls_PayOutFund.GetPayOutFundChilds(strType, ctbRsBack_no.Text.Trim());//重新绑定从表结果
                        if (dst != null && dst.Tables[0].Rows.Count > 0)
                        {
                            DataGrid dgrd = (DataGrid)this.dgrdResult.Items[intIndex].FindControl("dgrdChildResult");
                            this.BindStockInChildByDataView(dgrd, dst, 0);
                        }
                        //this.lblJs.Text = "<script>document.getElementById('" + Im_No.ClientID + "_txtBox').focus();</script>";
                    }
                    else
                    {
                        this.Page.RegisterClientScriptBlock("Js", "<script>alert('新增记录失败，请重试！')</script>");
                    }
                }
                else if (intCommandName == 5)//打开从表添加面板
                {
                    panChildAddResult.Visible = true;
                    CuTextSelectBox ctsbChildAdd0 = (CuTextSelectBox)this.dgrdResult.Items[intIndex].FindControl("ctsbChildAdd0");
                    //this.lblJs.Text = "<script>document.getElementById('" + ctsbChildAdd0.ClientID + "_txtBox').focus();</script>";
                }
            }
        }
        else
        {
            this.Page.Session.Remove("SetOrGetUserID");
            this.Page.RegisterClientScriptBlock("Js", "<script>parent.window.location.href='../index.aspx'</script>");
        }
    }

    public void UpDateStockInInfo(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();

        string strUserID = cls_Property.SetOrGetUserID;

        if (strUserID != "")
        {
            ImageButton ibtnUpdate = (ImageButton)sender;
            int intCommandName = Int32.Parse(ibtnUpdate.CommandName);
            int intIndex = Int32.Parse(ibtnUpdate.CommandArgument);

            if (intCommandName == 0)//主表确认
            {
                int intCount = this.dgrdResult.Items.Count;

                for (int i = 0; i < intCount; i++)
                {
                    bool IsChecked = ((CheckBox)this.dgrdResult.Items[i].FindControl("chkRsSelect")).Checked;

                    if (IsChecked)
                    {
                        string strBack_No = ((CuTextBox)this.dgrdResult.Items[i].FindControl("ctbRsBack_no")).Text.Trim();//采购退款单号

                        ClsPayOutFundDAL cls_PayOutFund = new ClsPayOutFundDAL();
                        Boolean bolIsSucceed = cls_PayOutFund.AuditStockPayOutFund(strBack_No, 0, strUserID, strType);

                        if (!bolIsSucceed)
                        {
                            this.Page.RegisterClientScriptBlock("Js", "<script>alert('确认失败，请重试！')</script>");
                        }
                    }
                }

                this.GetStockInInfoByMain();//重新绑定结果
            }
            else if (intCommandName == 3)//主表取消确认
            {
                string strBack_No = ((CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbRsBack_no")).Text.Trim();//采购退款单号

                ClsPayOutFundDAL cls_PayOutFund = new ClsPayOutFundDAL();
                Boolean bolIsSucceed = cls_PayOutFund.AuditStockPayOutFund(strBack_No, 1, strUserID, strType);

                if (bolIsSucceed)
                {
                    this.GetStockInInfoByMain();//重新绑定结果
                }
                else
                {
                    this.Page.RegisterClientScriptBlock("Js", "<script>alert('取消确认失败，请重试！')</script>");
                }
            }
            else if (intCommandName == 1 || intCommandName == 2 || intCommandName == 4)//主表编辑
            {
                CheckBox chkRsSelect = (CheckBox)this.dgrdResult.Items[intIndex].FindControl("chkRsSelect");
                ImageButton ibtnRsCancel = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsCancel");
                ImageButton ibtnRsEdit = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsEdit");
                ImageButton ibtnRsServer = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsServer");
                ImageButton ibtnRsOAudit = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsOAudit");

                CuTextBox Back_no = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbRsBack_no");//采购退款单号
                CuTextSelectBox Back_date = (CuTextSelectBox)this.dgrdResult.Items[intIndex].FindControl("ctsbRsBack_date");//退款日期 
                CuTextBox Remark = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbRsRemark");//备注
                         
                if (intCommandName == 1)//取消编辑
                {
                    this.GetStockInInfoByMain();//重新绑定结果
                }
                else if (intCommandName == 2)//编辑
                {                                 
                    ibtnRsCancel.Visible = ibtnRsServer.Visible = true;
                    chkRsSelect.Visible = ibtnRsEdit.Visible = ibtnRsOAudit.Visible = false;
                    Back_date.ReadOnly = Remark.ReadOnly = false;
                    //this.lblJs.Text = "<script>document.getElementById('" + ctsbRs0.ClientID + "_txtBox').select();document.getElementById('" + ctsbRs0.ClientID + "_txtBox').focus();</script>";
                }
                else//保存编辑
                {
                    ClsPayOutFundDAL cls_PayOutFund = new ClsPayOutFundDAL();
                    Boolean bolIsSucceed = cls_PayOutFund.UpdatePayOutFundM(strType, Back_no.Text.Trim(),
                        DateTime.Parse(Back_date.Text.Trim()), Remark.Text.Trim());                        

                    if (bolIsSucceed)
                    {
                        this.GetStockInInfoByMain();//重新绑定结果
                    }
                    else
                    {
                        this.Page.RegisterClientScriptBlock("Js", "<script>alert('修改记录失败，请重试！')</script>");
                    }
                }
            }
            //////////////////////////////////////////从表相关操作/////////////////////////////////////
            else
            {
                ClsPayOutFundDAL cls_PayOutFund = new ClsPayOutFundDAL();
                DataGrid dgrd = new DataGrid();
                string strBack_No = "";
                int intItems = this.dgrdResult.Items.Count;

                for (int i = intItems - 1; i >= 0; i--)//找当前可见的子DataGrid
                {
                    Panel panItem = (Panel)this.dgrdResult.Items[i].FindControl("panItem");

                    if (panItem.Visible)
                    {
                        dgrd = (DataGrid)this.dgrdResult.Items[i].FindControl("dgrdChildResult");
                        strBack_No = ((CuTextBox)this.dgrdResult.Items[i].FindControl("ctbRsBack_no")).Text.Trim();
                        break;
                    }
                }

                CheckBox chkChildRsSelect = (CheckBox)dgrd.Items[intIndex].FindControl("chkChildRsSelect");//在子DataGrid中找要操作的控件
                ImageButton ibtnChildRsCancel = (ImageButton)dgrd.Items[intIndex].FindControl("ibtnChildRsCancel");
                ImageButton ibtnChildRsEdit = (ImageButton)dgrd.Items[intIndex].FindControl("ibtnChildRsEdit");
                ImageButton ibtnChildRsServer = (ImageButton)dgrd.Items[intIndex].FindControl("ibtnChildRsServer");

                //CuTextBox ctbBack_no = (CuTextBox)dgrd.Items[intIndex].FindControl("ctbRsBack_no");//采购退款单号
                CuTextSelectBox ctsbBackNo = (CuTextSelectBox)dgrd.Items[intIndex].FindControl("ctsbChildRsBackNo");//采购退货单号
                CuTextBox ctbIm_no = (CuTextBox)dgrd.Items[intIndex].FindControl("ctbChildRsIm_no");//商品编号
                CuTextBox ctbBackMoney = (CuTextBox)dgrd.Items[intIndex].FindControl("ctbChildRsBackMoney");//可退金额
                CuTextBox ctbCurBackMoney = (CuTextBox)dgrd.Items[intIndex].FindControl("ctbChildRsCurBackMoney");//当前退款金额               
                CuTextBox ctbRate = (CuTextBox)dgrd.Items[intIndex].FindControl("ctsbChildRsRate");//费用
                CuTextBox ctbRemark = (CuTextBox)dgrd.Items[intIndex].FindControl("ctbChildRsRemark");//备注


                if (intCommandName == 5)//取消编辑
                {
                    //DataSet dst = cls_BackFund.GetBackFundChilds(strBack_No, "4");
                    DataSet dst = cls_PayOutFund.GetPayOutFundChilds(strType, strBack_No);

                    if (dst != null && dst.Tables[0].Rows.Count > 0)
                    {
                        this.BindStockInChildByDataView(dgrd, dst, 0);
                    }
                }
                else if (intCommandName == 6)//编辑
                {
                    chkChildRsSelect.Visible = ibtnChildRsEdit.Visible = false;
                    ibtnChildRsCancel.Visible = ibtnChildRsServer.Visible = true;
                    ctbCurBackMoney.ReadOnly = ctbRate.ReadOnly = false;
                    //this.lblJs.Text = "<script>document.getElementById('" + ctsbChildRs0.ClientID + "_txtBox').select();document.getElementById('" + ctsbChildRs0.ClientID + "_txtBox').focus();</script>";
                }
                else//保存编辑
                {
                    //string strN_No = dgrd.Items[intIndex].Cells[1].Text.Trim();
                    string strLot_no = ((Label)dgrd.Items[intIndex].FindControl("hidlot_no")).Text.Trim();//商品批号
                    decimal dBackMoney = decimal.Parse(ctbBackMoney.Text.Trim());//可退金额
                    decimal dCurBackMoney = decimal.Parse(ctbCurBackMoney.Text.Trim());//当前退款金额
                    decimal dRate = decimal.Parse(ctbRate.Text.Trim());//费用
                    if ((dCurBackMoney + dRate) > dBackMoney)
                    {
                        this.labMessage.Text = "<script language='javascript'>alert('退款金额+费用不能>可退金额！');</script>";
                        ctbCurBackMoney.ReadOnly = ctbRate.ReadOnly = false;
                        this.lblJs.Text = "<script>document.getElementById('" + ctbCurBackMoney.ClientID + "_txtBox').select();document.getElementById('" + ctbCurBackMoney.ClientID + "_txtBox').focus();</script>";
                    }
                    else
                    {
                        Boolean bolIsSucceed = cls_PayOutFund.UpdateStockBackFundChild(strType, strBack_No, ctsbBackNo.Text.Trim(),
                            ctbIm_no.Text.Trim(),strLot_no, dBackMoney, dCurBackMoney, dRate, ctbRemark.Text.Trim());

                        if (bolIsSucceed)//重新绑定从表结果
                        {
                            //DataSet dst = cls_BackFund.GetBackFundChilds(strBack_No, "4");
                            DataSet dst = cls_PayOutFund.GetPayOutFundChilds(strType, strBack_No);

                            if (dst != null && dst.Tables[0].Rows.Count > 0)
                            {
                                this.BindStockInChildByDataView(dgrd, dst, 0);
                            }
                        }
                        else
                        {
                            this.Page.RegisterClientScriptBlock("Js", "<script>alert('修改记录失败，请重试！')</script>");
                        }
                    }
                }
            }
        }
        else
        {
            this.Page.Session.Remove("SetOrGetUserID");
            this.Page.RegisterClientScriptBlock("Js", "<script>parent.window.location.href='../index.aspx'</script>");
        }
    }

    public void lbtnDelete_Click(object sender, EventArgs e)
    {
        int intCommandName = Int32.Parse(((LinkButton)sender).CommandName);

        this.DeleteStockCheckInfo(intCommandName);
    }

    public void OpenChildResult(object sender, ImageClickEventArgs e)
    {
        ClsPayOutFundDAL cls_PayOutFund = new ClsPayOutFundDAL();
        ImageButton ibtnOpen = (ImageButton)sender;
        int intCommandName = Int32.Parse(ibtnOpen.CommandName);
        int intIndex = Int32.Parse(ibtnOpen.CommandArgument);
        int intCount = this.dgrdResult.Items.Count;
        bool is_audit = bool.Parse(this.dgrdResult.Items[intIndex].Cells[1].Text);

        CheckBox chkRsSelect = (CheckBox)this.dgrdResult.Items[intIndex].FindControl("chkRsSelect");
        ImageButton ibtnOpenChild = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnOpenChild");
        ImageButton ibtnRsCancel = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsCancel");
        ImageButton ibtnRsEdit = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsEdit");
        ImageButton ibtnRsServer = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsServer");
        ImageButton ibtnRsOAudit = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsOAudit");
        Panel panShowChildResult = (Panel)this.dgrdResult.Items[intIndex].FindControl("panShowChildResult");

        CuTextBox ctbRsBack_no = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("ctbRsBack_no");//显示从表信息        
        DataSet dst = cls_PayOutFund.GetPayOutFundChilds(strType, ctbRsBack_no.Text.Trim());

        if (dst == null || dst.Tables[0].Rows.Count <= 0)
        {
            ibtnOpen.Attributes.Add("OnClick", "return confirm('单据录入未完成，您要删除此单据吗？');");
        }

        if (intCommandName == 0)//展开
        {
            if (dst != null && dst.Tables[0].Rows.Count > 0)
            {
                DataGrid dgrd = (DataGrid)this.dgrdResult.Items[intIndex].FindControl("dgrdChildResult");
                this.BindStockInChildByDataView(dgrd, dst, 0);//绑定从表信息
            }

            for (int i = 0; i < intCount; i++)//找到展开的项
            {
                Panel panItem = (Panel)this.dgrdResult.Items[i].FindControl("panItem");

                if (i == intIndex)
                {
                    panItem.Visible = true;
                }
                else
                {
                    panItem.Visible = false;
                }
            }

            this.ibtnAdd.CommandName = "5";//主表展开后则不可操作
            this.ibtnRef.CommandName = "1";
            this.ibtnAdd.CommandArgument = intIndex.ToString();
            panShowChildResult.Visible = true;
            ibtnOpenChild.CommandName = "1";
            ibtnOpenChild.ImageUrl = "../App_Images/ico/10.gif";
            ibtnRsEdit.ImageUrl = "../App_Images/ico/14.gif";
            ibtnRsServer.ImageUrl = "../App_Images/ico/16.gif";
            ibtnRsOAudit.ImageUrl = "../App_Images/ico/17.gif";
            ibtnRsCancel.ImageUrl = "../App_Images/ico/18.gif";
            this.ibtnAudit.ImageUrl = "../App_Images/b_21_3.gif";
            this.ibtnAudit.Enabled = chkRsSelect.Enabled = ibtnRsCancel.Enabled = ibtnRsEdit.Enabled = ibtnRsServer.Enabled = ibtnRsOAudit.Enabled = false;
            this.lblRsCounts.Enabled = this.lblPageCounts.Enabled = this.lblCurrPage.Enabled = this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = this.lblGoTxt1.Enabled = this.txtGo.Enabled = this.lblGoTxt2.Enabled = false;

            if (is_audit)//如果主表确认后则从表不可操作
            {
                this.ibtnAdd.Enabled = this.ibtnDelete0.Enabled = false;
                this.ibtnAdd.ImageUrl = "../App_Images/b_18_3.gif";
                this.ibtnDelete0.ImageUrl = "../App_Images/b_19_5.gif";
                this.imgDelete.ImageUrl = "../App_Images/b_19_6.gif";
                this.imgDelete.Attributes.Add("OnMouseOver", "");
                this.imgDelete.Attributes.Add("OnMouseOut", "");
                this.imgDelete.Attributes.Add("OnClick", "");
            }                        
        }
        else//收缩
        {
            this.ibtnAdd.CommandName = "0";//收缩后都可操作
            this.ibtnRef.CommandName = "0";
            panShowChildResult.Visible = false;
            ibtnOpenChild.CommandName = "0";
            ibtnOpenChild.ImageUrl = "../App_Images/ico/9.gif";
            ibtnRsEdit.ImageUrl = "../App_Images/ico/12.gif";
            ibtnRsServer.ImageUrl = "../App_Images/ico/11.gif";
            ibtnRsOAudit.ImageUrl = "../App_Images/ico/8.gif";
            ibtnRsCancel.ImageUrl = "../App_Images/ico/4.gif";
            this.ibtnAudit.ImageUrl = "../App_Images/b_21_1.gif";
            this.ibtnAdd.ImageUrl = "../App_Images/b_18_1.gif";
            this.ibtnDelete0.ImageUrl = "../App_Images/b_19_1.gif";
            this.imgDelete.ImageUrl = "../App_Images/b_19_4.gif";
            this.imgDelete.Attributes.Add("OnMouseOver", "ChangImage(this, '../App_Images/b_19_3.gif');ChangImage(document.all.ibtnDelete0, '../App_Images/b_19_2.gif')");
            this.imgDelete.Attributes.Add("OnMouseOut", "ChangImage(this, '../App_Images/b_19_4.gif');ChangImage(document.all.ibtnDelete0, '../App_Images/b_19_1.gif')");
            this.imgDelete.Attributes.Add("OnClick", "ChildMenuVisible()");
            this.ibtnAdd.Enabled = this.ibtnDelete0.Enabled = this.ibtnAudit.Enabled = chkRsSelect.Enabled = ibtnRsCancel.Enabled = ibtnRsEdit.Enabled = ibtnRsServer.Enabled = ibtnRsOAudit.Enabled = true;

            if (dst == null || dst.Tables[0].Rows.Count <= 0)
            {
                Boolean bolIsSucceed = cls_PayOutFund.DeleteStockBackFund(strType, ctbRsBack_no.Text.Trim(), "", "", "", 0, 0, 1);
                this.GetStockInInfoByMain();
            }
            else
            {
                //重新绑定
                ClsPropertyDAL cls_Property = new ClsPropertyDAL();
                dst = cls_Property.SetOrGetCorporationDataSet;
                this.BindStockInByDataView(dst, this.dgrdResult.CurrentPageIndex);
            }

            //this.GetStockInInfoByMain();
        }
    }

    private void GetStockInInfoByMain()
    {
        ClsPayOutFundDAL cls_PayOutFund = new ClsPayOutFundDAL();
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();

        string strUserID = cls_Property.SetOrGetUserID;
        DataSet dst = cls_PayOutFund.GetPayOutFundMains(strUserID,strType);

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {            
            cls_Property.SetOrGetCorporationDataSet = dst;
            this.BindStockInByDataView(dst, 0);
        }
        else
        {
            this.dgrdResult.CurrentPageIndex = 0;
            this.dgrdResult.DataSource = null;
            this.dgrdResult.DataBind();
            this.lblRsCounts.Text = "共0条记录";
            this.lblPageCounts.Text = "共0页";
            this.lblCurrPage.Text = "当前第0页";
            this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = this.txtGo.Enabled = false;
            this.lblRsCounts.Enabled = this.lblPageCounts.Enabled = this.lblCurrPage.Enabled = this.lblGoTxt1.Enabled = this.lblGoTxt2.Enabled = false;
        }
    }

    private void BindStockInByDataView(DataSet dst, int CurrPageIndex)
    {
        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            this.dgrdResult.CurrentPageIndex = CurrPageIndex;
            this.dgrdResult.DataSource = dst;
            this.dgrdResult.DataBind();

            this.lblRsCounts.Text = "共" + dst.Tables[0].Rows.Count + "条记录";
            this.lblPageCounts.Text = "共" + this.dgrdResult.PageCount + "页";
            this.lblCurrPage.Text = "当前第" + (this.dgrdResult.CurrentPageIndex + 1) + "页";
            this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = this.txtGo.Enabled = true;
            this.lblRsCounts.Enabled = this.lblPageCounts.Enabled = this.lblCurrPage.Enabled = this.lblGoTxt1.Enabled = this.lblGoTxt2.Enabled = true;

            if (this.dgrdResult.PageCount == 1)
            {
                this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = false;
                this.lblGoTxt1.Enabled = this.txtGo.Enabled = this.lblGoTxt2.Enabled = false;
            }
            else if (this.dgrdResult.CurrentPageIndex == this.dgrdResult.PageCount - 1)
            {
                this.lbtnNext.Enabled = this.lbtnEnd.Enabled = false;
            }
            else if (this.dgrdResult.CurrentPageIndex == 0)
            {
                this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = false;
            }
        }
        else
        {
            this.dgrdResult.DataSource = null;
            this.dgrdResult.DataBind();        
        }
    }

    private void BindStockInChildByDataView(DataGrid dgrd, DataSet dst, int CurrPageIndex)
    {
        if (dgrd != null && dst != null)
        {
            dgrd.CurrentPageIndex = CurrPageIndex;
            dgrd.DataSource = dst;
            dgrd.DataBind();

            int intIndex = Int32.Parse(dgrd.ToolTip);
            int intItems = dgrd.Items.Count;

            Label lblChildRsCounts = (Label)this.dgrdResult.Items[intIndex].FindControl("lblChildRsCounts");
            Label lblChildPageCounts = (Label)this.dgrdResult.Items[intIndex].FindControl("lblChildPageCounts");
            Label lblChildCurrPage = (Label)this.dgrdResult.Items[intIndex].FindControl("lblChildCurrPage");
            LinkButton lbtnChildFirst = (LinkButton)this.dgrdResult.Items[intIndex].FindControl("lbtnChildFirst");
            LinkButton lbtnChildPrev = (LinkButton)this.dgrdResult.Items[intIndex].FindControl("lbtnChildPrev");
            LinkButton lbtnChildNext = (LinkButton)this.dgrdResult.Items[intIndex].FindControl("lbtnChildNext");
            LinkButton lbtnChildEnd = (LinkButton)this.dgrdResult.Items[intIndex].FindControl("lbtnChildEnd");

            lblChildRsCounts.Text = "共" + dst.Tables[0].Rows.Count + "条明细记录";
            lblChildPageCounts.Text = "共" + dgrd.PageCount + "页";
            lblChildCurrPage.Text = "当前第" + (dgrd.CurrentPageIndex + 1) + "页";
            lbtnChildFirst.Enabled = lbtnChildPrev.Enabled = lbtnChildNext.Enabled = lbtnChildEnd.Enabled = true;
            lblChildRsCounts.Enabled = lblChildPageCounts.Enabled = lblChildCurrPage.Enabled = true;

            if (dgrd.PageCount == 1)
            {
                lbtnChildFirst.Enabled = lbtnChildPrev.Enabled = lbtnChildNext.Enabled = lbtnChildEnd.Enabled = false;
            }
            else if (dgrd.CurrentPageIndex == dgrd.PageCount - 1)
            {
                lbtnChildNext.Enabled = lbtnChildEnd.Enabled = false;
            }
            else if (dgrd.CurrentPageIndex == 0)
            {
                lbtnChildFirst.Enabled = lbtnChildPrev.Enabled = false;
            }

            //判断从表中的记录是否可操作
            //for (int i = 0; i < intItems; i++)
            //{
            //    CheckBox chkChildRsSelect = (CheckBox)dgrd.Items[i].FindControl("chkChildRsSelect");
            //    ImageButton ibtnChildRsEdit = (ImageButton)dgrd.Items[i].FindControl("ibtnChildRsEdit");

            //    if (!this.ibtnAdd.Enabled)
            //    {
            //        chkChildRsSelect.Enabled = ibtnChildRsEdit.Enabled = false;
            //        ibtnChildRsEdit.ImageUrl = "../App_Images/ico/14.gif";
            //    }
            //    else
            //    {
            //        chkChildRsSelect.Enabled = ibtnChildRsEdit.Enabled = true;
            //        ibtnChildRsEdit.ImageUrl = "../App_Images/ico/12.gif";
            //    }
            //}

            //保存从表中的结果集和DataGrid控件用于从表中的翻页
            ClsPropertyDAL cls_Property = new ClsPropertyDAL();
            cls_Property.SetOrGetChildResultDataSet = dst;
            cls_Property.SetOrGetChildDataGridControl = dgrd;
        }
    }

    private void DeleteStockCheckInfo(int CommandName)
    {
        ClsPayOutFundDAL cls_PayOutFund = new ClsPayOutFundDAL();
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        string strUserID = cls_Property.SetOrGetUserID;

        if (strUserID != "")
        {
            ClsBackFundDAL cls_BackFund = new ClsBackFundDAL();
            int intCount = this.dgrdResult.Items.Count;
            int intcommand = Int32.Parse(this.ibtnRef.CommandName);

            if (intcommand == 0)//删除主表
            {
                for (int i = 0; i < intCount; i++)
                {
                    bool IsChecked = ((CheckBox)this.dgrdResult.Items[i].FindControl("chkRsSelect")).Checked;

                    if (IsChecked)
                    {
                        Boolean bolIsSucceed = false;
                        string strBack_No = ((CuTextBox)this.dgrdResult.Items[i].FindControl("ctbRsBack_no")).Text.Trim();
                        
                        if (CommandName == 0)//不彻底删除
                        {
                            bolIsSucceed = cls_PayOutFund.DeleteStockBackFund(strType, strBack_No, "", "", "", 0, 0, 0);
                        }
                        else//彻底删除
                        {
                            bolIsSucceed = cls_PayOutFund.DeleteStockBackFund(strType, strBack_No, "", "", "", 0, 0, 1);
                        }

                        if (!bolIsSucceed)
                        {
                            this.Page.RegisterClientScriptBlock("Js", "<script>alert('删除记录失败，请重试！')</script>");
                        }                        
                    }
                }

                //重新绑定主表结果
                this.GetStockInInfoByMain();
            }
            else//删除从表
            {
                DataGrid dgrd = new DataGrid();
                string strBack_No = "";
                int intItems = this.dgrdResult.Items.Count;

                for (int i = intItems - 1; i >= 0; i--)//找当前可见的子DataGrid
                {
                    Panel panItem = (Panel)this.dgrdResult.Items[i].FindControl("panItem");

                    if (panItem.Visible)
                    {
                        dgrd = (DataGrid)this.dgrdResult.Items[i].FindControl("dgrdChildResult");
                        strBack_No = ((CuTextBox)this.dgrdResult.Items[i].FindControl("ctbRsBack_no")).Text.Trim();
                        break;
                    }
                }

                int intChildItems = dgrd.Items.Count;//循环删除所选的项

                for (int j = 0; j < intChildItems; j++)
                {
                    bool IsChecked = ((CheckBox)dgrd.Items[j].FindControl("chkChildRsSelect")).Checked;

                    if (IsChecked)
                    {
                        //string strN_No = dgrd.Items[j].Cells[1].Text.Trim();
                        string strVm_No = ((CuTextSelectBox)dgrd.Items[j].FindControl("ctsbChildRsBackNo")).Text.Trim();//采购退货单号
                        string strIm_no = ((CuTextBox)dgrd.Items[j].FindControl("ctbChildRsIm_no")).Text.Trim();//商品编号
                        string strLot_no = ((Label)dgrd.Items[j].FindControl("hidlot_no")).Text.Trim();//商品批号
                        decimal dCur_money = decimal.Parse(((CuTextBox)dgrd.Items[j].FindControl("ctbChildRsCurBackMoney")).Text.Trim());//退款金额
                        decimal dCur_Rate = decimal.Parse(((CuTextBox)dgrd.Items[j].FindControl("ctsbChildRsRate")).Text.Trim());//费用
                        Boolean bolIsSucceed = false;

                        if (CommandName == 0)//不彻底删除
                        {
                            bolIsSucceed = cls_PayOutFund.DeleteStockBackFund(strType, strBack_No, strVm_No, strIm_no, strLot_no, dCur_money, dCur_Rate, - 1);
                        }
                        else//彻底删除
                        {
                            bolIsSucceed = cls_PayOutFund.DeleteStockBackFund(strType, strBack_No, strVm_No, strIm_no, strLot_no, dCur_money, dCur_Rate, - 2);
                        }

                        if (!bolIsSucceed)
                        {
                            this.Page.RegisterClientScriptBlock("Js", "<script>alert('删除记录失败，请重试！')</script>");
                        }
                    }
                }

                DataSet dst = cls_PayOutFund.GetPayOutFundChilds(strType, strBack_No);//重新绑定从表结果

                if (dst != null && dst.Tables[0].Rows.Count > 0)
                {
                    this.BindStockInChildByDataView(dgrd, dst, 0);
                }
                else
                {
                    dgrd.DataSource = null;
                    dgrd.DataBind();
                }
            }
        }
        else
        {
            this.Page.Session.Remove("SetOrGetUserID");
            this.Page.RegisterClientScriptBlock("Js", "<script>parent.window.location.href='../index.aspx'</script>");
        }
    }

    private void ControlButtonVisible(string UserLogID, string CurrPageID)
    {
        ClsMenuDAL cls_Menu = new ClsMenuDAL();
        DataSet dst = cls_Menu.GetCurrUserButtonPopedom(UserLogID, CurrPageID);

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            int intCount = dst.Tables[0].Rows.Count - 1;

            for (int i = intCount; i >= 0; i--)
            {
                int intButtonID = Int32.Parse(dst.Tables[0].Rows[i][2].ToString());

                switch (intButtonID)
                {
                    case 0:
                        this.panAdd.Visible = true;
                        break;
                    case 1:
                        this.hidButtonEdit.Value = intButtonID.ToString();
                        break;
                    case 2:
                        this.panDelete.Visible = true;
                        break;
                    case 3:
                        this.lbtnDelete1.Enabled = true;
                        break;
                    case 4:
                        this.panAudit.Visible = true;
                        break;
                    case 5:
                        this.hidButtonOAudit.Value = intButtonID.ToString();
                        break;
                    case 6:
                        this.panRef.Visible = true;
                        break;
                    case 7:
                        this.panSearch.Visible = true;
                        break;
                }
            }
        }
    }    

    protected void ibtnDelete0_Click(object sender, ImageClickEventArgs e)
    {
        int intCommandName = Int32.Parse(((ImageButton)sender).CommandName);

        this.DeleteStockCheckInfo(intCommandName);
    }

    protected void txtGo_TextChanged(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        DataSet dst = cls_Property.SetOrGetCorporationDataSet;

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            string strIndex = this.txtGo.Text.Trim();
            this.txtGo.Text = String.Empty;

            if (strIndex != "")
            {
                try
                {
                    int intIndex = Int32.Parse(strIndex) - 1;

                    if (intIndex >= 0 && intIndex < this.dgrdResult.PageCount)
                    {
                        this.BindStockInByDataView(dst, intIndex);
                    }
                    else
                    {
                        this.Page.RegisterClientScriptBlock("Js", "<script>alert('请输入正确的页码数！')</script>");
                    }
                }
                catch
                {
                    this.Page.RegisterClientScriptBlock("Js", "<script>alert('请输入数字类型的页码！')</script>");
                }
            }
            else
            {
                this.Page.RegisterClientScriptBlock("Js", "<script>alert('请输入要跳转的页码！')</script>");
            }
        }
    }

    protected void ibtnRef_Click(object sender, ImageClickEventArgs e)
    {
        int intCommand = Int32.Parse(((ImageButton)sender).CommandName);

        if (intCommand == 0)//刷新主表
        {
            this.GetStockInInfoByMain();
        }
        else//刷新从表
        {
            DataGrid dgrd = new DataGrid();
            string strBack_No = "";
            int intItems = this.dgrdResult.Items.Count;

            for (int i = intItems - 1; i >= 0; i--)//找当前可见的子DataGrid
            {
                Panel panItem = (Panel)this.dgrdResult.Items[i].FindControl("panItem");

                if (panItem.Visible)
                {
                    dgrd = (DataGrid)this.dgrdResult.Items[i].FindControl("dgrdChildResult");
                    strBack_No = ((CuTextBox)this.dgrdResult.Items[i].FindControl("ctbRsBack_no")).Text.Trim();
                    break;
                }
            }

            ClsPayOutFundDAL cls_PayOutFund = new ClsPayOutFundDAL();
            DataSet dst = cls_PayOutFund.GetPayOutFundChilds(strType, strBack_No);

            if (dst != null && dst.Tables[0].Rows.Count > 0)
            {
                this.BindStockInChildByDataView(dgrd, dst, 0);
            }
        }
    }

    protected void dgrdResult_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemIndex != -1)
        {
            bool Is_audit = bool.Parse(e.Item.Cells[1].Text.Trim());
            Boolean bIs_visible = Boolean.Parse(e.Item.Cells[2].Text.Trim());//是否作废

            CheckBox chkRsSelect = (CheckBox)e.Item.FindControl("chkRsSelect");
            CuTextBox ctbRsRemark = (CuTextBox)e.Item.FindControl("ctbRsRemark");
            ImageButton ibtnRsEdit = (ImageButton)e.Item.FindControl("ibtnRsEdit");
            ImageButton ibtnRsCancel = (ImageButton)e.Item.FindControl("ibtnRsCancel");
            ImageButton ibtnRsServer = (ImageButton)e.Item.FindControl("ibtnRsServer");
            ImageButton ibtnRsOAudit = (ImageButton)e.Item.FindControl("ibtnRsOAudit");

            //修改按回车提交
            if (ctbRsRemark.ReadOnly)
            {
                ctbRsRemark.Attributes.Add("onkeydown", "if(event.keyCode == 13){document.getElementById('" + ibtnRsServer.ClientID + "').click();}");
            }

            if (bIs_visible)//是否作废
            {
                chkRsSelect.Enabled = ibtnRsEdit.Enabled = false;
                ibtnRsOAudit.Enabled = false;
                ibtnRsEdit.ImageUrl = "../App_Images/ico/14.gif";
                //ibtnRsOAudit.ImageUrl = "../App_Images/ico/17.gif";
            }

            string strButtonEdit = this.hidButtonEdit.Value;
            string strButtonOAudit = this.hidButtonOAudit.Value;

            if (!Is_audit)
            {
                if (strButtonEdit.Trim() == "1")
                {
                    if (this.panAudit.Visible || this.panDelete.Visible)
                    {
                        chkRsSelect.Visible = true;
                    }

                    ibtnRsEdit.Visible = true;
                }
                else if (this.panAudit.Visible || this.panDelete.Visible)
                {
                    chkRsSelect.Visible = true;
                }
            }
            else
            {
                ibtnRsOAudit.Visible = true;

                if (strButtonOAudit.Trim() != "5")
                {
                    ibtnRsOAudit.Enabled = false;
                    ibtnRsOAudit.ImageUrl = "../App_Images/ico/17.gif";
                }
            }
        }
    }

    protected void dgrdResult_ItemCreated(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType.ToString() == "Item" || e.Item.ItemType.ToString() == "AlternatingItem")
        {
            DataGrid dgrdChildResult = (DataGrid)e.Item.FindControl("dgrdChildResult");

            //子DataGrid注册事件
            dgrdChildResult.ItemDataBound += new DataGridItemEventHandler(dgrdChildResult_ItemDataBound);
        }
    }

    protected void dgrdChildResult_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemIndex != -1)
        {
            int iIndex = int.Parse(((DataGrid)sender).ToolTip);
            bool Is_audit = bool.Parse(this.dgrdResult.Items[iIndex].Cells[1].Text.Trim());//是否确认
            bool Is_visible = bool.Parse(this.dgrdResult.Items[iIndex].Cells[2].Text.Trim());//是否作废
            CheckBox chkChildRsSelect = (CheckBox)e.Item.FindControl("chkChildRsSelect");
            ImageButton ibtnChildRsEdit = (ImageButton)e.Item.FindControl("ibtnChildRsEdit");
            if (Is_audit || Is_visible)//是否作废
            {
                chkChildRsSelect.Enabled = ibtnChildRsEdit.Enabled = false;
                ibtnChildRsEdit.ImageUrl = "../App_Images/ico/14.gif";
            }
        }
    }

    //根据订单状态进行相关查询
    public void lbtnQuery_Click(object sender, EventArgs e)
    {
        int intCommandName = Int32.Parse(((LinkButton)sender).CommandName);

        this.QueryOrderByStatu(intCommandName);
    }

    private void QueryOrderByStatu(int CommandName)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        string strUserID = cls_Property.SetOrGetUserID;

        if (strUserID != "")
        {
            ClsPayOutFundDAL cls_PayOutFund = new ClsPayOutFundDAL();
            DataSet dst = cls_PayOutFund.GetOrderInformationByStatu(strUserID, "tboStockPayorBackFundM", int.Parse(strType), CommandName);

            cls_Property.SetOrGetCorporationDataSet = dst;
            this.BindStockInByDataView(dst, 0);
        }
    }       
}
